package sys.spvisor.console.controller;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import sys.spvisor.core.entity.journal.TJournal;
import sys.spvisor.core.service.journal.JournalService;
import sys.spvisor.core.util.JournalFileUtil;

@Controller
@RequestMapping("/journal")
public class JournalController {

	@Autowired
	private JournalService journalService;

	@RequestMapping("/getJournal.ajax")
	public @ResponseBody Map<String, Object> selectAllJournal(int page, int rows,String sort,String order) {
		System.out.println("_________________________"+sort+"  "+order+"_____________________________");
		//System.out.println(journalService.getAllJournalByPageInfo(rows, start));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", journalService.getCountJournal());
		result.put("list", journalService.getAllJournalByPageInfo(rows, (page-1)*rows));
		return result;
	}

	@RequestMapping("/downloadJournal.ajax")
	public ResponseEntity<byte[]> download(String filename, String name, HttpServletRequest request)
			throws IOException {

		System.out.println(filename+"  "+name);
		String filePath = request.getSession().getServletContext().getRealPath("upload");
		String sufix = name.substring(name.lastIndexOf("."));
		String dfileName = new String((filename + sufix).getBytes("UTF-8"), "iso8859-1");
		System.out.println(dfileName);
		File file = new File(filePath, name);
		if(file.exists()){
			System.out.println("is");
		}
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", dfileName);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);

	}

	@RequestMapping("/addJournal.ajax")
	public @ResponseBody Map<String, Object> upload(
			@RequestParam(value = "tempfile", required = false) MultipartFile file, TJournal tJournal,
			HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Date date = new Date(System.currentTimeMillis());
		File targetFile = JournalFileUtil.createJournalFileName(request, file);
		tJournal.setPosttime(date);
		tJournal.setFile(targetFile.getName());
		tJournal.setStatus(0);

		// 保存
		try {
			int tag = journalService.insertJournal(tJournal, file, targetFile);
			if (tag == 0) {
				result.put("success", false);
				result.put("message", "日志上重复上传！");
			} else if (tag == 1) {
				result.put("success", true);
				result.put("message", "日志上传成功！");
			} else {
				result.put("success", false);
				result.put("message", "日志上传失败！");
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("message", "日志上传失败！");
		}

		return result;
	}

	/*
	 * 编辑日志 主要是对附件的更新操作
	 */
	@RequestMapping("/editJournal.ajax")
	public @ResponseBody Map<String, Object> editJournal(
			@RequestParam(value = "tempfile", required = false) MultipartFile file, TJournal tJournal,
			HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		System.out.println("开始");
		String fileParentPath = request.getSession().getServletContext().getRealPath("upload");
		;
		// 保存
		try {

			journalService.editJournal(tJournal, file, fileParentPath);
			result.put("success", true);
			result.put("message", "日志上传成功！");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("message", "日志上传失败！");
		}

		return result;
	}

	/*
	 * 根据时间 项目 周次 类型 获取日志内容
	 */
	@RequestMapping("/viewJournal.ajax")
	public @ResponseBody Map<String, Object> viewJournalByPTT(TJournal tJournal, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		System.out.println("开始");
		List<TJournal> list = journalService.getJournalByTime(tJournal);

		// 保存
		try {

			if (list != null && !list.isEmpty()) {
				result.put("success", true);
				result.put("journal", list.get(0));

			} else {
				result.put("success", false);
				result.put("message", "获取数据失败成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("message", "获取数据失败成功！");
		}

		return result;
	}
}
